var back = angular.module('back', [
    'ngAnimate',
    'ngCookies',
    'ngResource',
    'ngSanitize',
    'ngStorage',
    'ui.router',
    'ui.bootstrap',
    'api.srv',
    'ui.load',
    'ui.jq',
    'ui.validate',
    'oc.lazyLoad'
])

.run(['$rootScope', '$state', '$stateParams', function($rootScope, $state, $stateParams) {
    $rootScope.$state = $state;
    $rootScope.$stateParams = $stateParams;
}])

.config(['$controllerProvider', '$compileProvider', '$filterProvider', '$provide',
    function($controllerProvider, $compileProvider, $filterProvider, $provide) {

        // 根据访问的页面动态加载Controller
        back.controller = $controllerProvider.register;
        back.directive = $compileProvider.directive;
        back.filter = $filterProvider.register;
        back.factory = $provide.factory;
        back.service = $provide.service;
        back.constant = $provide.constant;
        back.value = $provide.value;
    }
])

.config(function($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise('/back/promote');
    $stateProvider
        .state('back', {
            abstract: true,
            url: '/back',
            templateUrl: 'tpl/back.html'
        })

    // 推广管理
    .state('back.promote', {
        url: '/promote',
        templateUrl: 'tpl/promote.html',
        resolve: {
            deps: ['$ocLazyLoad',
                function($ocLazyLoad) {
                    return $ocLazyLoad.load(['static/js/controllers/promote.js']);
                }
            ]
        }
    })

    // 财务管理
    .state('back.finance', {
        url: '/finance',
        template: '<div ui-view class="fade-in"></div>',
        resolve: {
            deps: ['uiLoad', function(uiLoad) {
                return uiLoad.load('static/js/controllers/finance.js');
            }]
        }
    })

    // 财务管理 - 充值
    .state('back.finance.recharge', {
        url: '/recharge',
        templateUrl: 'tpl/finance.recharge.html'
    })

    // 财务管理 - 财务记录
    .state('back.finance.record', {
        url: '/finance',
        templateUrl: 'tpl/finance.record.html'
    })

    // 图片管理
    .state('back.img', {
        url: '/img',
        template: '<div ui-view class="fade-in"></div>',
        resolve: {
            deps: ['uiLoad', function(uiLoad) {
                return uiLoad.load('static/js/controllers/content.img.js');
            }]
        }
    })

    .state('back.img.static', {
        url: '/static',
        templateUrl: 'tpl/img.static.html'
    })
})

.constant('config', {
    host: 'http://127.0.0.1:3000/',
    noop: function() {}
})

.constant('JQ_CONFIG', {
    dataTable: ['static/js/vendor/jquery/datatables/jquery.dataTables.min.js',
        'static/js/vendor/jquery/datatables/dataTables.bootstrap.js',
        'static/js/vendor/jquery/datatables/dataTables.bootstrap.css'
    ]
})

.controller('BackCtrl', ['$scope', '$localStorage', '$window', function($scope, $localStorage, $window) {
    /* 增加ie样式 */
    var isIE = navigator.userAgent.match(/MSIE/i);
    isIE && angular.element($window.document.body).addClass('ie');
    isSmartDevice($window) && angular.element($window.document.body).addClass('smart');

    // 配置
    $scope.app = {
        name: '理财宝',
        version: '0.0.1',
        // 图标颜色
        color: {
            primary: '#7266ba',
            info: '#23b7e5',
            success: '#27c24c',
            warning: '#fad733',
            danger: '#f05050',
            light: '#e8eff0',
            dark: '#3a3f51',
            black: '#1c2b36'
        },
        settings: {
            themeID: 1,
            navbarHeaderColor: 'bg-black',
            navbarCollapseColor: 'bg-white-only',
            asideColor: 'bg-black',
            headerFixed: true,
            asideFixed: false,
            asideFolded: false,
            asideDock: false,
            container: false
        }
    }

    // 本地存储设置
    if (angular.isDefined($localStorage.settings)) {
        $scope.app.settings = $localStorage.settings;
    } else {
        $localStorage.settings = $scope.app.settings;
    }
    $scope.$watch('app.settings', function() {
        if ($scope.app.settings.asideDock && $scope.app.settings.asideFixed) {
            $scope.app.settings.headerFixed = true;
        }
        $localStorage.settings = $scope.app.settings;
    }, true);

    // 判断是不是小屏幕设备 
    function isSmartDevice($window) {
        var ua = $window['navigator']['userAgent'] || $window['navigator']['vendor'] || $window['opera'];
        return (/iPhone|iPod|iPad|Silk|Android|BlackBerry|Opera Mini|IEMobile/).test(ua);
    }

}])
